package njust.page;

import njust.utils.DBUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class Page {
    private int pageNo;
    private List<Map<String,Object>> list;
    private int pageSize;
    private int totalSize;
    private int prevPage;
    private int nextPage;
    public Page(){}

    public Page(int pageNo, List<Map<String, Object>> list, int pageSize, int totalSize, int prevPage, int nextPage) {
        this.pageNo = pageNo;
        this.list = list;
        this.pageSize = pageSize;
        this.totalSize = totalSize;
        this.prevPage = prevPage;
        this.nextPage = nextPage;
    }

    public int getPageNo() {
        return pageNo;
    }

    public List<Map<String,Object>> getList() {
        return list;
    }

    public int getPageSize() {
        return pageSize;
    }

    public int getTotalSize() {
        return totalSize;
    }

    public int getPrevPage() {
        return prevPage;
    }

    public int getNextPage() {
        return nextPage;
    }

    //对不同的表，查询总共的数据元素个数
    //
    public static int analyseTotalSize(String tableName,int pageSize){
        try {
            List<Map<String, Object>> list = DBUtil.query(tableName, null);
            if (list.size()%pageSize==0){
                return list.size()/pageSize;
            }else {
                return list.size()/pageSize+1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public boolean ifHaveNext(){
        if (pageNo<totalSize){
            return true;
        }
        else
            return false;
    }

    public boolean ifHavPre(){
        if (pageNo>1){
            return true;
        }
        else
            return false;
    }

    //listAll为查询得到的整个list表
    public static List<Page> createPagesByList(String tableName,int pageSize){
        int totalSize=Page.analyseTotalSize(tableName,pageSize);
        List<Page> pageList=new ArrayList<>();
        List<Map<String,Object>> list=new ArrayList<>();    //list为对应page页的数据list表
        int pageNum=1;
        for (;pageNum<=totalSize;pageNum++){
            String limit=(pageNum-1)*pageSize+","+pageSize;
            try {
                list = DBUtil.query(tableName, false, null, null, null, null, null, null, limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            Page page=new Page(pageNum,list,pageSize,totalSize,pageNum-1,pageNum+1);
            pageList.add(page);
        }
        return pageList;
    }

    //order为排列顺序，递增为""，递减为"desc"
    public static List<Page> orderPage(String tableName,int pageSize,String order){
        int totalSize=Page.analyseTotalSize(tableName,pageSize);
        List<Page> pageList=new ArrayList<>();
        List<Map<String,Object>> list=new ArrayList<>();    //list为对应page页的数据list表
        int pageNum=1;
        for (;pageNum<=totalSize;pageNum++){
            String limit=(pageNum-1)*pageSize+","+pageSize;
            try {
                list = DBUtil.query(tableName, false, null, null, null, null, null, "cNum "+order, limit);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            Page page=new Page(pageNum,list,pageSize,totalSize,pageNum-1,pageNum+1);
            pageList.add(page);
        }
        return pageList;
    }

}
